{% extends 'myadmin/public/base.html' %}

{% block title %}
<title>后台商品列表</title>
{% endblock %}


{% block header %}
    <div class="row">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-9">
            <div class="page-header-heading"><span class="am-icon-home page-header-heading-icon"></span>商品列表</div>
            <p class="page-header-description">Amaze UI </p>
        </div>
    </div>
{% endblock %}
 

{% block con %}
<div class="row-content am-cf">
    <div class="row">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
            <div class="widget am-cf">
                <div class="widget-head am-cf">
                    <div class="widget-title  am-cf">商品列表</div>


                </div>
                <div class="widget-body  am-fr">

                    <div class="am-u-sm-12 am-u-md-6 am-u-lg-6">
                        <div class="am-form-group">
                            <div class="am-btn-toolbar">
                                <div class="am-btn-group am-btn-group-xs">
                                    <button type="button" class="am-btn am-btn-default am-btn-success"><span class="am-icon-plus"></span> 新增</button>
                                    <button type="button" class="am-btn am-btn-default am-btn-secondary"><span class="am-icon-save"></span> 保存</button>
                                    <button type="button" class="am-btn am-btn-default am-btn-warning"><span class="am-icon-archive"></span> 审核</button>
                                    <button type="button" class="am-btn am-btn-default am-btn-danger"><span class="am-icon-trash-o"></span> 删除</button>
                                </div>
                            </div>
                        </div>
                    </div>

                <form>
                    <div class="am-u-sm-12 am-u-md-6 am-u-lg-3">
                        <div class="am-form-group tpl-table-list-select">
                            <select name="type" data-am-selected="{btnSize: 'sm'}" style="display: none;">
           
                              <option value="username" {% if request.GET.type == 'name' %} selected {% endif %} value="name">分类名</option>
                              <
                            </select>
                        </div>
                    </div>
                    <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
                        <div class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
                            <input type="text" name="keywords" class="am-form-field " value="{{ request.GET.keywords }}">
                            <span class="am-input-group-btn">
                            <button class="am-btn  am-btn-default am-btn-success tpl-table-list-field am-icon-search"></button>
                          </span>
                        </div>
                    </div>
                </form>


                    <div class="am-u-sm-12">
                        <table width="100%" class="am-table am-table-compact am-table-striped tpl-table-black ">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>分类名</th>
                                    <th>所属分类</th>
                                    <th>分类路径</th>                           
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                            {% for v in typelist %}
                                <tr class="gradeX">
                                    <td class="am-text-middle">{{ v.id }}</td>

                                    <td class="am-text-middle editname" tid="{{ v.id }}">
                                        {% if v.pid != 0 %}
                                            &nbsp;&nbsp;<span>|----</span>
                                        {% endif %}

                                        <span class="name">{{v.name }}</span>
                                    </td>


                                    <td class="am-text-middle">{{ v.pname|default:'一级分类' }}</td>
                                    <td class="am-text-middle">{{ v.path }}</td>
                                                            
                                    <td class="am-text-middle">
                                        <div class="tpl-table-black-operation">
                                            <a href="javascript:;">
                                                <i class="am-icon-pencil"></i> 编辑
                                            </a>
                                            <a href="javascript:;"  sid="{{ v.id }}"  class="tpl-table-black-operation-del">
                                                <i class="am-icon-trash"></i> 删除
                                            </a>
                                        </div>
                                    </td>
                                </tr>

                                
                            {% endfor %}
                        
                                <!-- more data -->
                            </tbody>
                        </table>
                    </div>
                    <div class="am-u-lg-12 am-cf">
                        <div class="am-fr">
                            <ul class="am-pagination tpl-pagination">
                              

                            {% load pagetag %}

                            {% showpage typelist.paginator.num_pages request %}     
                                        
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    // 商品数据的删除

    // 给删除的按钮 绑定单击事件
    $('.tpl-table-black-operation-del').click(function(){
        // 获取id 否则删除谁
        var id = $(this).attr('sid')
        var btn = $(this)

        // 发送ajax请求， 执行数据的删除
        // ajax的请面2个数据-- '',{} 是给views的，function的参数才是views返回的,所以叫做回调函数
        $.get('{% url "myadmin_types_delete" %}', {id:id}, function(data){
            if(data['code'] == 0){
                
                btn.parents('tr').remove()
            }
            alert(data['msg'])
        },'json')
    })



    $('.editname').dblclick(function(){
        // 先获取当前的分类名
        var name = $(this).find('.name').text()
        var t = $(this)
        // 把td中的分类名的内容换成一个 input
        var inp = $('<input type="text" value="'+name+'" style="color:black;">')
        $(this).find('.name').html(inp)
        // 设置当前input的内容直接选中
        inp.select()
        // 给input绑定丧失焦点事件
        inp.blur(function(){
            // 获取当前的值
            var newname = $(this).val()
            // 判断是否更新了分类名
            if(newname == name){
                // 恢复原样
                $(this).remove();//删除input
                t.find('.name').text(name)
            }else{
                // 获取分类id newname id
                var tid = t.attr('tid')
                
                //发送ajax,修改数据 
                $.get('{% url "myadmin_types_update" %}',{id:tid,name:newname},function(data){
                    console.log(data)
                    t.find('.name input').remove()
                    if(data['code'] == 0){
                        // 成功,设置新的分类名
                        t.find('.name').text(newname)
                    }else{
                        // 恢复原样
                        t.find('.name').text(name)
                    }
                },'json')

                
            }
        })


    })
</script>
{% endblock %}